from flask import Flask, config, request
from flask_socketio import SocketIO
from common.log.logUtils import logUtils
from serversRtc.rtcMsgWorker import serverRtcMsgWorker

##配置nginx
# https://blog.csdn.net/hfismyangel/article/details/82758629

app = Flask(__name__, static_folder='staticResources', static_url_path='/resources')

app.config.from_object(config)
# 增加跨域请求
socketio = SocketIO(app, cors_allowed_origins='*')

rtcMsgWorker = serverRtcMsgWorker()


# 加入房间
@socketio.on('join')
def enterRoom(roomId,clientType):
    sid = request.sid

    logUtils.info("enterRoom param:sid=" + sid + ",roomId=" + roomId+"/clientType:"+str(clientType))

    rtcMsgWorker.enterRoom(sid, roomId,clientType)


@socketio.on('message')
def onMessage(roomId, data,clientType):

    rtcMsgWorker.dealMessage(roomId, data,clientType);


@socketio.on('leave')
def leaveRoom(roomId):
    sid = request.sid

    logUtils.info("enterRoom param:sid=" + sid + ",roomId=" + roomId)

    rtcMsgWorker.leaveRoom(sid, roomId)


if __name__ == "__main__":
    logUtils.info("start service......")
    socketio.run(app,host="0.0.0.0")
